{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "from tensorflow.examples.tutorials.mnist import input_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Extracting MNIST_data\\train-images-idx3-ubyte.gz\n",
      "Extracting MNIST_data\\train-labels-idx1-ubyte.gz\n",
      "Extracting MNIST_data\\t10k-images-idx3-ubyte.gz\n",
      "Extracting MNIST_data\\t10k-labels-idx1-ubyte.gz\n",
      "iter: 0,Testing Accuracy 0.8248\n",
      "iter: 1,Testing Accuracy 0.8938\n",
      "iter: 2,Testing Accuracy 0.9018\n",
      "iter: 3,Testing Accuracy 0.9071\n",
      "iter: 4,Testing Accuracy 0.9085\n",
      "iter: 5,Testing Accuracy 0.9104\n",
      "iter: 6,Testing Accuracy 0.9117\n",
      "iter: 7,Testing Accuracy 0.9125\n",
      "iter: 8,Testing Accuracy 0.915\n",
      "iter: 9,Testing Accuracy 0.9163\n",
      "iter: 10,Testing Accuracy 0.9161\n",
      "iter: 11,Testing Accuracy 0.9185\n",
      "iter: 12,Testing Accuracy 0.918\n",
      "iter: 13,Testing Accuracy 0.9198\n",
      "iter: 14,Testing Accuracy 0.9196\n",
      "iter: 15,Testing Accuracy 0.9203\n",
      "iter: 16,Testing Accuracy 0.9214\n",
      "iter: 17,Testing Accuracy 0.921\n",
      "iter: 18,Testing Accuracy 0.9205\n",
      "iter: 19,Testing Accuracy 0.9216\n",
      "iter: 20,Testing Accuracy 0.9215\n"
     ]
    }
   ],
   "source": [
    "#载入数据集\n",
    "mnist = input_data.read_data_sets(\"MNIST_data\",one_hot=True)\n",
    "\n",
    "\n",
    "#定义每批次的大小\n",
    "batch_size=100\n",
    "#计算一共有多少批次\n",
    "n_batch = mnist.train.num_examples // batch_size\n",
    "#命名空间\n",
    "with tf.name_scope('input'):\n",
    "    #定义变量\n",
    "    x = tf.placeholder(tf.float32,[None,784])\n",
    "    y = tf.placeholder(tf.float32,[None,10])\n",
    "\n",
    "#创建神经网络\n",
    "W = tf.Variable(tf.zeros([784,10]))\n",
    "b = tf.Variable(tf.zeros([10]))\n",
    "predict = tf.nn.softmax(tf.matmul(x,W)+b)\n",
    "# predict = tf.nn.sigmoid(tf.matmul(x,W)+b)\n",
    "#代价函数\n",
    "#loss = tf.reduce_mean(tf.square(y-predict))\n",
    "loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=predict))\n",
    "#梯度下降\n",
    "train_step = tf.train.GradientDescentOptimizer(0.2).minimize(loss)\n",
    "\n",
    "#初始化变量\n",
    "init = tf.global_variables_initializer()\n",
    "\n",
    "#验证结果\n",
    "correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(predict,1))\n",
    "#正确率\n",
    "accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))\n",
    "\n",
    "with tf.Session() as sess:\n",
    "    sess.run(init)\n",
    "    for epoch in range(21):\n",
    "        for batch in range(n_batch):\n",
    "            batch_x,batch_y = mnist.train.next_batch(batch_size)\n",
    "            sess.run(train_step,feed_dict={x:batch_x,y:batch_y})\n",
    "        \n",
    "        #计算正确率\n",
    "        acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})\n",
    "        print('iter: '+str(epoch)+',Testing Accuracy '+ str(acc))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    ""
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3.0
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}